#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5,A=1e6+5;
int col[A],coler[A],nxt[N];
ll a[N],sum[N],dp[N];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    scanf("%d",&t);
    for(int o=1;o<=t;o++){
        int n;
        scanf("%d",&n);
        memset(col,0,sizeof(col));
        memset(coler,0,sizeof(coler));
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            nxt[i]=col[a[i]];
            col[a[i]]=i;
            if(!coler[a[i]]) coler[a[i]]=i;
        }
        sum[0]=0;
        for(int i=1;i<=n;i++){
            sum[i]=sum[i-1];
            if(a[i]==a[i-1]) sum[i]+=a[i];
        }
        dp[0]=0;
        for(int i=1;i<=n;i++){
            dp[i]=max(dp[i-1],sum[i]);
            if(nxt[i]>0){
                ll s=sum[i-1]-sum[nxt[i]];
                if(coler[a[nxt[i]+1]]<nxt[i]) s+=a[nxt[i]+1];
                dp[i]=max(dp[i],dp[nxt[i]]+s+a[i]);
            }
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}
